<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item>
        <el-form-item label="选择楼栋" prop="dId">
          <el-cascader
            v-model="testvalue"
            :options="downtownList"
            :props="cateProps"
            placeholder="请输入楼栋名称"
            @change="handleChange"
          ></el-cascader>
        </el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="getShopsById(this.queryParams.id)"
          >搜索</el-button
        >
        <!--        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>-->
      </el-form-item>
    </el-form>

    <div class="cardbody" style="margin-top: 20px; margin-bottom: 20px">
      <div class="cardhead" style="display: flex; align-items: center">
        <div
          class="point"
          style="
            height: 10px;
            width: 10px;
            border-radius: 5px;
            background-color: #909399;
          "
        ></div>
        <span style="margin-left: 5px">空置</span>
        <div
          class="point"
          style="
            height: 10px;
            width: 10px;
            border-radius: 5px;
            background-color: #1890ff;
          "
        ></div>
        <span style="margin-left: 5px">已出租</span>
        <div
          class="point"
          style="
            height: 10px;
            width: 10px;
            border-radius: 5px;
            background-color: #20b2aa;
          "
        ></div>
        <span style="margin-left: 5px">已出售</span>
        <div
          class="point"
          style="
            height: 10px;
            width: 10px;
            border-radius: 5px;
            background-color: #e6a23c;
          "
        ></div>
        <span style="margin-left: 5px">即将到期</span>
        <div
          class="point"
          style="
            height: 10px;
            width: 10px;
            border-radius: 5px;
            background-color: #f56c6c;
          "
        ></div>
        <span style="margin-left: 5px">租约逾期</span>
      </div>
    </div>
    <div
      v-for="(item, index) in this.shopsfloor"
      :key="item.id"
      class="shopcard"
    >
      <div class="floorinfo" style="width: 100px; text-align: center">
        <div class="leftfloor">{{ item }}层</div>
      </div>
      <div class="shopInfo" style="width: 90%">
        <el-card
          :body-style="{ padding: 0 }"
          :key="indexs"
          class="box-card"
          shadow="hover"
          style="width: 250px; height: 100px"
          v-for="(item, indexs) in shopsLength[index]"
          @click.native="openDialog(shopsInfoList[index][indexs].id)"
        >
          <div class="headone" v-if="shopsInfoList[index][indexs].state == 0">
            <div style="line-height: 40px; margin-left: 10px">
              编号：{{ shopsInfoList[index][indexs].code }}({{
                shopsInfoList[index][indexs].floorArea
              }}㎡)
            </div>
          </div>
          <div
            class="shopdowninfo"
            v-if="shopsInfoList[index][indexs].state == 0"
          >
            <div class="carddown">
              门牌号：{{ shopsInfoList[index][indexs].room }}
            </div>
            <div class="carddown">
              备注：{{ shopsInfoList[index][indexs].remark }}
            </div>
          </div>

          <div class="headtwo" v-if="shopsInfoList[index][indexs].state == 1">
            <div style="line-height: 40px; margin-left: 10px">
              编号：{{ shopsInfoList[index][indexs].code }}({{
                shopsInfoList[index][indexs].floorArea
              }}㎡)
            </div>
          </div>
          <div
            class="shopdowninfo"
            v-if="shopsInfoList[index][indexs].state == 1"
          >
            <div class="carddown">
              门牌号：{{ shopsInfoList[index][indexs].room }}
            </div>
            <div class="carddown">
              备注：{{ shopsInfoList[index][indexs].remark }}
            </div>
          </div>

          <div class="headthree" v-if="shopsInfoList[index][indexs].state == 2">
            <div style="line-height: 40px; margin-left: 10px">
              编号：{{ shopsInfoList[index][indexs].code }}({{
                shopsInfoList[index][indexs].floorArea
              }}㎡)
            </div>
          </div>
          <div
            class="shopdowninfo"
            v-if="shopsInfoList[index][indexs].state == 2"
          >
            <div class="carddown">
              门牌号：{{ shopsInfoList[index][indexs].room }}
            </div>
            <div class="carddown">
              备注：{{ shopsInfoList[index][indexs].remark }}
            </div>
          </div>

          <div class="headfore" v-if="shopsInfoList[index][indexs].state == 3">
            <div style="line-height: 40px; margin-left: 10px">
              编号：{{ shopsInfoList[index][indexs].code }}({{
                shopsInfoList[index][indexs].floorArea
              }}㎡)
            </div>
          </div>
          <div
            class="shopdowninfo"
            v-if="shopsInfoList[index][indexs].state == 3"
          >
            <div class="carddown">
              门牌号：{{ shopsInfoList[index][indexs].room }}
            </div>
            <div class="carddown">
              备注：{{ shopsInfoList[index][indexs].remark }}
            </div>
          </div>

          <div class="headfive" v-if="shopsInfoList[index][indexs].state == 4">
            <div style="line-height: 40px; margin-left: 10px">
              编号：{{ shopsInfoList[index][indexs].code }}({{
                shopsInfoList[index][indexs].floorArea
              }}㎡)
            </div>
          </div>
          <div
            class="shopdowninfo"
            v-if="shopsInfoList[index][indexs].state == 4"
          >
            <div class="carddown">
              门牌号：{{ shopsInfoList[index][indexs].room }}
            </div>
            <div class="carddown">
              备注：{{ shopsInfoList[index][indexs].remark }}
            </div>
          </div>
        </el-card>
      </div>
    </div>
    <!--    点击查看Shop详细信息对话框-->
    <el-dialog title="门店信息" :visible.sync="dialogVisible" width="30%">
      <el-descriptions class="margin-top" :column="1" border>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-user"></i>
            门牌号:
          </template>
          {{ this.shopInfo.room }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-mobile-phone"></i>
            编号:
          </template>
          {{ this.shopInfo.code }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-location-outline"></i>
            占地面积:
          </template>
          {{ this.shopInfo.floorArea + "㎡" }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-tickets"></i>
            使用面积:
          </template>
          {{ this.shopInfo.useArea + "㎡" }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-office-building"></i>
            所属楼栋:
          </template>
          {{ this.shopInfo.downtownName }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-office-building"></i>
            所属商业区：
          </template>
          {{ this.shopInfo.bName }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-office-building"></i>
            租户信息：
          </template>
          {{ hirerInfo.name }}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            <i class="el-icon-office-building"></i>
            租户手机号：
          </template>
          {{ hirerInfo.phone }}
        </el-descriptions-item>
      </el-descriptions>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogVisible = false"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  listTongji,
  getTongji,
  delTongji,
  addTongji,
  updateTongji,
  listbuils,
  listShopsById,
  getShops,
} from "@/api/system/tongji";

import { getHirer } from "@/api/renter/hirer";
import { listRentalcontract } from "@/api/rentalcontract/rentalcontract";
export default {
  name: "Tongji",
  data() {
    return {
      hirerInfo: {},
      //控制显示与隐藏
      dialogVisible: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 租赁统计信息管理表格数据
      tongjiList: [],
      downtownList: [],
      cateProps: {
        label: "name",
        value: "id",
        children: "zhwyBuildingList",
      },
      testvalue: [3, 1],
      shopsfloor: {},
      shopsInfo: [],
      shopsInfoList: [],
      shopsLength: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        id: "",
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      shopInfo: "",
    };
  },
  created() {
    this.getbuileList();
    // this.getShopsById(1001);
  },
  methods: {
    // 打开详细店铺信息对话框
    openDialog(id) {
      console.log(id);
      this.shopInfo = "";
      getShops(id).then((response) => {
        this.shopInfo = response.data;
        listRentalcontract({ shopsId: this.shopInfo.id }).then((res) => {
          getHirer(res.rows[0].hirerId).then((hireRes) => {
            this.hirerInfo = hireRes.data;
            this.dialogVisible = true;
          });
        });
      });
    },
    // 关闭对话框
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          done();
        })
        .catch((_) => {});
    },
    // 根据楼栋id获取所有商铺信息
    getShopsById(id) {
      this.shopsLength = [];
      this.loading = true;
      // console.log(id)
      listShopsById(id).then((response) => {
        this.shopsfloor = response.data.ceng;
        this.shopsInfoList = response.data.sp;
        //通过循环商铺，将每个楼层对应的商铺个数，存储shopsLength数组。
        for (let i = 0; i < this.shopsInfoList.length; i++) {
          this.shopsLength.push(this.shopsInfoList[i].length); //将每个楼层中的商铺个数放入数组。
        }
        this.loading = false;
      });
    },

    handleChange(testvalue) {
      console.log(testvalue);
      this.queryParams.id = testvalue[1];
      this.getShopsById(testvalue[1]);
      console.log(testvalue[1]);
    },
    /** 查询楼栋信息管理列表 */
    getbuileList() {
      this.loading = true;
      listbuils(this.queryParams).then((response) => {
        this.downtownList = response.rows;
        // this.total = response.total;
        this.getShopsById(this.downtownList[0].zhwyBuildingList[0].id);
        this.loading = false;
      });
    },
    /** 查询租赁统计信息管理列表 */
    getList() {
      this.loading = true;
      listTongji(this.queryParams).then((response) => {
        this.tongjiList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    // resetQuery() {
    //   this.resetForm("queryForm");
    //   this.handleQuery();
    // },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加租赁统计信息管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getTongji(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改租赁统计信息管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateTongji(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTongji(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除租赁统计信息管理编号为"' + ids + '"的数据项？')
        .then(function () {
          return delTongji(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/tongji/export",
        {
          ...this.queryParams,
        },
        `tongji_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
<style scoped>
.cardhead .point {
  margin-left: 10px;
}
.shopcard {
  display: flex;
  margin-bottom: 10px;
}

.shopcard .leftfloor {
  width: 50px;
  height: 50px;
  border: solid 1px #ccc;
  margin: 10px auto;
  line-height: 50px;
  box-shadow: #ccc 5px 5px;
}
.shopcard .shopInfo {
  display: flex;
  /*justify-content: space-around;*/
  flex-wrap: wrap;
}

.shopcard .headone {
  width: 250px;
  height: 40px;
  background-color: #909399;
  margin-bottom: 5px;
}
.shopcard .headtwo {
  width: 250px;
  height: 40px;
  background-color: #1890ff;
  margin-bottom: 5px;
}
.shopcard .headthree {
  width: 250px;
  height: 40px;
  background-color: #20b2aa;
  margin-bottom: 5px;
}
.shopcard .headfore {
  width: 250px;
  height: 40px;
  background-color: #e6a23c;
  margin-bottom: 5px;
}
.shopcard .headfive {
  width: 250px;
  height: 40px;
  background-color: #f56c6c;
  margin-bottom: 5px;
}
.shopcard .carddown {
  margin-left: 10px;
}

.shopcard .box-card {
  /*margin-bottom: 5px;*/
  --n: 4;
  --gap: calc((100% - 250px * var(--n)) / var(--n) / 2);
  margin: 10px var(--gap);
  border-radius: 5px;
}
</style>
